var app = getApp()
Page({
	data: {
    index:0,
		hidden:true,
		curNav:1,
    rice:0,
		curIndex:0,
		cart:[],
    steamed_bun:0,
		cartTotal:0,
    currentId:0,
    price_count:0,
		dishesList:
			[
         "无","一荤一素"
		],
		dishes:[]
	},
	loadingChange () {
		setTimeout(() => {
			this.setData({
				hidden:true
			})
		},2000)
	},
	selectNav (event) {
		let id = event.target.dataset.id,
			index = parseInt(event.target.dataset.index);
			self = this;
		this.setData({
			curNav:id,
			curIndex:index
		})
	},
  plus_sb:function(event){
    this.setData({
      steamed_bun: this.data.steamed_bun+1
    })
    this.refreshPrices()
  },
  sub_sb:function(event){
   let count=(this.data.steamed_bun -1)
    this.setData({
      steamed_bun: count < 0 ? 0 : count
    })
    this.refreshPrices()
  },
  plus_rice: function (event) {
    this.setData({
      rice: this.data.rice + 1
    })
    this.refreshPrices()
  },
  sub_rice: function (event) {
    let count = (this.data.rice - 1)
    this.setData({
      rice: count < 0 ? 0 : count
    })
    this.refreshPrices()
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
    this.refreshPrices()
  },
   refreshPrices :function(){
    var that=this
     this.setData({
       price_count: that.data.price_count+1
     })
    if(this.data.index==1){
      this.setData({
        price_count: (8 + (that.data.rice * 0.5) + (that.data.steamed_bun * 0.5))
      })
    }else{
      this.setData({
        price_count: ((that.data.rice * 0.5) + (that.data.steamed_bun * 0.5))
      })
    }
  },
	// 选择菜品
	setStatus (dishId) {
		let dishes = this.data.dishesList;
		for (let dish of dishes){
			dish.forEach((item) => {
				if(item.id == dishId){
					item.status = !item.status || false
				}else{
          item.status=false
        }
			})
		}
		
		this.setData({
			dishesList:this.data.dishesList
		})
	},
  submit_order:function(){
    var that=this;
    var app=getApp()
      wx.request({
        url: 'https://thtl.vip/jishu/requestOrder',
        data:{
          userId: app.globalData.userId,
          dishCount:that.data.index,
          sbCount:that.data.steamed_bun,
          riceCount:that.data.rice,
          allCount: that.data.price_count
        },
        success:function(e){
          if(e.data==2){
            that.setData({
              tips: "今天您已提交过"
            })
            return
          }
          if(e.data==1){
          that.setData({
            tips:"提交成功"
          })
          }else{
            that.setData({
              tips: "失败"
            })
          }
        }
      })
  },
	onLoad () {
		// this.loadingChange()
	}
})